package com.qyl.hrm.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qyl.hrm.common.PageConstant;
import com.qyl.hrm.dto.Dept;
import com.qyl.hrm.dto.User;
import com.qyl.hrm.service.DeptService;
import com.qyl.hrm.utils.PageBean;

@Controller
@RequestMapping("/admin/dept")
public class DeptController {

	@Autowired
	private DeptService deptService;
	
	@RequestMapping("/deptList")
	public String deptList(Dept dept,Integer pageIndex,Integer pageSize,ModelMap map){
		
		//调用service查询总记录数
		Integer recordCount = deptService.getRecordCount(dept);
		
		PageBean pageBean = new PageBean(pageIndex, recordCount, pageSize);
		
		//调用service获得带有user列表的pageBean
		deptService.getDeptList(dept,pageBean);
		
		//返回数据到页面
		map.put("pageBean", pageBean);		
		map.put("backDept", dept);
		
		return "dept/deptList";		
	}
	
	@RequestMapping("/toAddDept")
	public String toAddDept(){
		
		return "dept/addDept";	
	}
	/**
	 * 保存用户
	 */
	@RequestMapping("/addDept")
	public String addDept(Dept dept,ModelMap map){
				
		int count = deptService.addDept(dept);		
		
		if(count > 0){
			map.put(PageConstant.TIP,"添加部门成功");
		}else{
			map.put(PageConstant.TIP,"添加部门失败");			
		}	
		return "dept/addDept";
	}
	
	/*
	 * 删除用户
	 */
	@RequestMapping("/deleteDept")
	public String deleteDept(Integer id,ModelMap map){
		
		int count = deptService.deleteDept(id);		
		
		if(count > 0){
			map.put(PageConstant.TIP,"删除部门成功");
		}else{
			map.put(PageConstant.TIP,"删除部门失败");			
		}			
		return "forward:deptList";
	}
	
	@RequestMapping("/toUpdateDept")
	public String toUpdateDept(Integer id,ModelMap map){
		
		Dept dept = deptService.getDeptById(id);
		
		map.put("dept", dept);
		
		return "dept/updateDept";
	}
	
	@RequestMapping("/updateDept")
	public String updateDept(Dept dept,ModelMap map){
		
		int count = deptService.updateDept(dept);		
		
		if(count > 0){
			map.put(PageConstant.TIP,"更新部门成功");
		}else{
			map.put(PageConstant.TIP,"更新部门失败");			
		}			
		
		return "forward:toUpdateDept";
	}	
}
